package com.SkyAI.test.socket;

import java.net.*;
import java.io.*;
import java.lang.*;

public class SocketClient  {
	private String _ip = null;
	private int _port = 0;
	private int iNum = 0;
	private Socket cSocket = null;
	private OutputStream os = null;
	private byte[] oBuff = null;
	private DataInputStream is = null;

	int oSize = 0;
	
	public SocketClient(String ip, int port){
		this._ip = ip;
		this._port = port;
	}
	
	public int Connecting() {
		try {
			// 创建通讯并且和主机Rock连接
			cSocket = new Socket(_ip, _port);
			// 打开这个Socket的输入/输出流
			os = cSocket.getOutputStream();
			is = new DataInputStream(cSocket.getInputStream());
			oSize = cSocket.getReceiveBufferSize();
			oBuff = new byte[oSize];
		} catch (Exception e) {
			System.out.println("Exception :" + e.getMessage());
		}
		return 1;
	}
	
	public int SendData(byte[] bData){
		try {
				int iSize = bData.length;
				int times = oSize / iSize;
				iNum++;
//				System.out.print("[" + iSize + "];[" + times + "];[" + iNum + "]");
				int iChar = 0;
				int iTimes = 0;
				while( times > iTimes ){
					oBuff[iChar] = bData[iChar%iSize];
					iTimes++;
					iChar++;
				}
				os.write(oBuff);
//				System.out.print("\r\n");
				return iTimes;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return 1;
	}
	
	public int RecvData(byte[] bRecv){
		try {
			return is.read(bRecv);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.getMessage();
			e.printStackTrace();
		}
		return -1;
	}
}
